function  writeSubjData(fs)

fid = fopen(['DvorakData.txt'],'w');
fprintf(fid,'Time\tSubjectiveData\tLoudnessCF\tLoudnessMGB\tAweightedSPL\tParticipant\n');
tree = dataStorageTree;
files = {...
  'DvorakOrig',...
  'DvorakInvert',...
  'DvorakOrigRetro',...
  'DvorakInvertRetro',...
  'BandsA1Silence09s',...
  'BandsA2Silence09s',...
  'BandsA3Silence09s',...
  'BandsB1Silence09s',...
  'BandsB2Silence09s',...
  'BandsB3Silence09s',...
  'BandsC1Silence09s',...
  'BandsC2Silence09s',...
  'BandsC3Silence09s',...
  'BandsD1Silence09s',...
  'BandsD2Silence09s',...
  'BandsD3Silence09s',...
  'BandsE1Silence09s',...
  'BandsE2Silence09s',...
  'BandsE3Silence09s',...
  'BandsF1Silence09s',...
  'BandsF2Silence09s',...
  'BandsF3Silence09s'};



for j = 1:4
  writeSubjDataFile(files{j} , fs, fid, tree);
end
fclose(fid);


function [out, maxcorr, maxlag] = writeSubjDataFile(file,fs,fid,tree)
if ~exist('exportsubjdata')
  exportsubjdata = 0;
end
  
disp(file);
rownum = 1;


% Get Median Subjective Data
[tsArray,data,time]  = subjDataForFile(file, fs,  tree);
medData = median(data,2);
stdErr = std(data') / sqrt(size(data,2));


% Get SLM
dsArr = get(tree,'Name','SPL A-weighted Fast','audiofile',file);
tsSLM = getData(dsArr,1);
tsSLM = resample(tsSLM.DataObj.tsObj, tsArray{1}.DataObj.Time);


% % Get ThirdOctave Spectral Centroid
% dsArr = get(tree,'Name','Spectral Centroid','audiofile',file,'analyserName','CPBFFT');
% tsCPBSC = getData(dsArr,1);
% tsCPBSC = resample(tsCPBSC.DataObj.tsObj, tsArray{1}.DataObj.Time);

% 
% Get Loudness C&F
 dsArr = get(tree,'Name','Loudness','audiofile',file,'analyserName','LoudnessCF');
 tsDLM= getData(dsArr,1);
 tsDLM = resample(tsDLM.DataObj.tsObj, tsArray{1}.DataObj.Time); 

% Get Loudness MG&B
dsArr = get(tree,'Name','Loudness','audiofile',file,'analyserName','LoudnessMGB');
tsMGB= getData(dsArr,1);
tsMGB = resample(tsMGB.DataObj.tsObj, tsArray{1}.DataObj.Time); 



for j = 1:length(tsArray)
len = min([ length(tsArray{j}.DataObj.Time) length(tsArray{j}.DataObj.Data) length(medData) length(tsDLM.Data) length(tsSLM.Data) ]);
for i = 1:len
  fprintf(fid,'%.2f\t%.2f\t%.2f\t%.2f\t%s\n',...
    tsArray{j}.DataObj.Time(i),...
    tsArray{j}.DataObj.Data(i),...
    tsDLM.Data(i),...
    tsMGB.Data(i),...
    tsSLM.Data(i),...
    tsArray{j}.DataObj.Name(strfind(tsArray{j}.DataObj.Name,'-')+1:end));
end
end
 


function [tsArray,data,time] = subjDataForFile(file,fs,tree)

% If this is a data folder, then everything below it is from the same 
% analyser folder. 

t = get(tree,'audiofile',file,'analyserName','Subjective Data');
st = convertToDataObjects(t);
col=1;
for i = 1:length(st)
  if isempty(strfind(st{i}.DataObj.Name,'Median Subjective Response'))
    ts = st{i};
    tvec = 0:1/fs:ts.DataObj.TimeInfo.End;
    ts.DataObj.tsObj = resample(ts.DataObj.tsObj,tvec);
    if length(ts.DataObj.Data)/fs ~= 54 
     tsArray{col} =  ts;
     col=col+1;
    end
  end
end
time = ts.DataObj.tsObj;
x=1;y=1;
for i = 1:length(tsArray)
  try
  data(:,i) = tsArray{i}.DataObj.Data;
    disp([num2str(length(tsArray{i}.DataObj.Data)) 'is a correct length for' tsArray{i}.DataObj.Name]);
  x= x+1;
  catch
    disp([num2str(length(tsArray{i}.DataObj.Data)) 'is a different length for' tsArray{i}.DataObj.Name]);
  
    y=y+1;
  end
end




